const path = require('path');
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const merge = require('webpack-merge');
const WebpackZipPlugin = require('zip-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
//const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
//const  package = require('../../package.json');
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
const ManifestPlugin = require('webpack-assets-manifest');
let OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
let TerserPlugin = require('terser-webpack-plugin');
const baseWebpackConfig = require('./webpack.config.base');
let fs = require('fs'); //无需安装，直接使用
const { project } = require('../build/project');

function resolve(dir) {
  return path.join(__dirname, '..', dir);
}

module.exports = merge(baseWebpackConfig, {
  mode: 'production',
  resolve: {
    extensions: ['.web.js', '.js', '.jsx', '.json', '.less', '.css'],
    alias: {
      env: resolve('../src/config/env/production')
    }
  },
  output: {
    publicPath: './',
    path: resolve('../dist/' + project),
    filename: 'static/[name].js',
    chunkFilename: 'static/routes/[name].chunk.js' // 按需加载输出的文件名
  },
  devtool: false,
  optimization: {
    minimizer: [
      new TerserPlugin({
        parallel: true,
      }),
    ],
  },
  plugins: [
    /* new UglifyJsPlugin({
         uglifyOptions: {
             compress: {
                 warnings: false,
                 drop_debugger: true,
                 drop_console: true
             },
             sourceMap: true
         }
     }),*/
    new OptimizeCSSPlugin({
      cssProcessorOptions: {
        safe: true
      }
    }),
    // 拷贝 static 文件夹资源文件
    new CopyWebpackPlugin([
      {
        from: resolve('../static'),
        to: 'static',
        ignore: ['.*']
      }
    ]),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV),
      },
    }),
  ]
});
